﻿namespace Movies.ViewModel
{
    using AllocineClient;
    using MoviesDataBase;

    /// <summary>
    /// The cover view model.
    /// </summary>
    public class CoverViewModel
    {
        #region Attributs

        /// <summary>
        /// The allocine parser.
        /// </summary>
        private AllocineParser allocineParser;

        /// <summary>
        /// The movie.
        /// </summary>
        private Movie movie;

        /// <summary>
        /// The data provider.
        /// </summary>
        private DataProvider dbase;

        /// <summary>
        /// The path of the selected movie.
        /// </summary>
        private string path;

        /// <summary>
        /// The title of the movie.
        /// </summary>
        private string title;

        #endregion

        #region Constructeurs

        /// <summary>
        /// Initializes a new instance of the <see cref="CoverViewModel"/> class.
        /// </summary>
        /// <param name="title">The title.</param>
        /// <param name="path">The path.</param>
        public CoverViewModel(string title, string path)
        {
            this.path = path;
            this.title = title;
        }

        public CoverViewModel(Movie movie)
        {
            this.path = movie.Path;
            this.title = movie.Title;
            this.Movie = movie;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the dbase.
        /// </summary>
        /// <value>
        /// The dbase.
        /// </value>
        public DataProvider Dbase
        {
            get
            {
                if (this.dbase == null)
                {
                    this.dbase = DataProvider.Instance;
                }

                return this.dbase;
            }

            set
            {
                this.dbase = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is downloading.
        /// </summary>
        /// <value>
        /// <c>True</c> if this instance is downloading; otherwise, <c>false</c>.
        /// </value>
        public bool IsDownloading { get; set; }

        /// <summary>
        /// Gets or sets the movie.
        /// </summary>
        /// <value>
        /// The movie.
        /// </value>
        public Movie Movie
        {
            get
            {
                if (this.movie == null)
                {
                    this.movie = this.Dbase.GetMovie(m => m.Title == this.title || m.Path == this.path);
                    if (this.movie == null)
                    {
                        this.movie = new Movie();
                        this.movie.Title = this.title;
                        this.movie.Path = this.path;
                        this.Dbase.AddMovie(this.movie);

                        this.AllocineParser = new AllocineParser(this.movie);
                        this.AllocineParser.DownloadMovieInformationAsync();
                    }
                }

                return this.movie;
            }

            set
            {
                this.movie = value;
            }
        }

        /// <summary>
        /// Gets or sets the allocine parser.
        /// </summary>
        /// <value>
        /// The allocine parser.
        /// </value>
        public AllocineParser AllocineParser
        {
            get
            {
                return this.allocineParser;
            }

            set
            {
                this.allocineParser = value;
                this.allocineParser.StartDownloading += (s, e) => { this.movie.IsDownloading = true; };
                this.allocineParser.DownloadEnded += (s, e) =>
                {
                    this.movie.IsDownloading = false;
                    this.movie.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(OnMoviePropertyChanged);
                    if (string.IsNullOrEmpty(this.movie.Poster))
                    {
                        try
                        {
                            this.movie.Poster = Resources.Resources.DefaultIconUri;
                        }
                        catch
                        {
                        }
                    }
                };
            }
        }

        #endregion

        #region Méthodes

        /// <summary>
        /// Called when [movie property changed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> instance containing the event data.</param>
        private void OnMoviePropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "AllocineUrl" && !string.IsNullOrEmpty(this.Movie.AllocineUrl))
            {
                this.AllocineParser.UpdateDataFromUrl(this.Movie.AllocineUrl);
            }
        }

        #endregion
    }
}
